﻿using JNPF.Basics.Logic;
using JNPF.Basics.Logic.WorkFlow.FlowFlowDynamic;
using JNPF.Basics.Models.WorkFlow.Dtos.FlowTask;
using JNPF.Utils;
using JNPF.Utils.Controller;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;

namespace JNPF.Basics.Controller.WorkFlow
{
    /// <summary>
    /// 动态表单
    /// 版 本：V3.0.0
    /// 版 权：引迈信息技术有限公司（https://www.jnpfsoft.com）
    /// 作 者：JNPF开发平台组
    /// 日 期：2018-01-17 
    /// </summary>
    [Route("api/WorkFlow/[controller]")]
    public class FlowTaskController : BaseApiController
    {
        private FlowTaskBll flowTaskBll = new FlowTaskBll();
        private FlowFlowDynamicBll flowFlowDynamicBll = new FlowFlowDynamicBll();

        #region Get
        /// <summary>
        /// 信息
        /// </summary>
        /// <param name="id">主键值</param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public async Task<ResponseResult> GetInfo(string id)
        {
            var flowEntity = await flowTaskBll.GetInfo(id);
            var output = await flowFlowDynamicBll.Info(flowEntity);
            return Success(output);
        }
        #endregion

        #region Post
        /// <summary>
        /// 新建
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResponseResult> Create([FromBody] FlowTaskCrInput input)
        {
            try
            {
                if (input.status == 1)
                {
                    await flowFlowDynamicBll.Save(null, input.flowId, input.data);
                    return Success("保存成功");
                }
                else
                {
                    await flowFlowDynamicBll.Submit(null, input.flowId, input.data, input.freeApproverUserId);
                    return Success("提交成功，请耐心等待");
                }
            }
            catch (Exception ex)
            {
                return Error(ex.Message);
            }
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="id">主键值</param>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        [HttpPut("{id}")]
        public async Task<ResponseResult> Update(string id, [FromBody] FlowTaskUpInput input)
        {
            try
            {
                if (input.status == 1)
                {
                    await flowFlowDynamicBll.Save(id, input.flowId, input.data);
                    return Success("保存成功");
                }
                else
                {
                    await flowFlowDynamicBll.Submit(id, input.flowId, input.data, input.freeApproverUserId);
                    return Success("提交成功，请耐心等待");
                }
            }
            catch (Exception ex)
            {
                return Error(ex.Message);
            }
        }
        #endregion
    }
}
